package sample;

import java.awt.Color;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import robocode.DadosRobos.DadosRobos;
import robocode.DadosRobos.RoboScaneado;
import robocode.AdvancedRobot;
import robocode.BattleEndedEvent;
import robocode.DeathEvent;
import robocode.HitRobotEvent;
import robocode.HitWallEvent;
import robocode.RobotDeathEvent;
import robocode.ScannedRobotEvent;
import robocode.StatusEvent;
import robocode.WinEvent;
//import tcp.TCPClient;
import tcp.TCPClient;
import tcp.TCPServer;
import tcp.interfaces.IRoboTCP;
import tcp.pacotes.AnalisePacotes;
import tcp.pacotes.CriadorPacotes;

/** 
 * @author Emerson Shigueo Sugimoto
 * @author Lucas Del Castanhel
 * */
public class RoboPatrulha extends AdvancedRobot {
	
	private int _portaServidorTCPJason = 7770;
	private String _ipTCPJason = "localhost";
	private HashMap<String, RoboScaneado> _robosScaneados = new HashMap<String, RoboScaneado>();
	
	private TCPServer _server = null;
	
	public RoboPatrulha() { }
	
	private int getIndiceRobo(String nomeRobo){
		if (nomeRobo.lastIndexOf('(') > 0)
			return Integer.valueOf(nomeRobo.substring(nomeRobo.lastIndexOf('(')+1, nomeRobo.lastIndexOf(')')));
		return 1;
	}
	
	private void addRoboScaneado( RoboScaneado rb ){
		if (_robosScaneados.containsKey(rb.getNomeRobo())) {
			_robosScaneados.remove(rb.getNomeRobo());
		}
		_robosScaneados.put(rb.getNomeRobo(), rb);
	}
	
	public void run(){
		setBodyColor(Color.WHITE);
		setGunColor(Color.BLACK);
		setRadarColor(Color.BLUE);
		setBulletColor(Color.green);
		setScanColor(Color.GREEN);
		
		turnRight(0 - getHeading()); //arruma a posicao inicial do robo
		ahead(- getY()+ getHeight()/2);
		turnRight(90 - getHeading());
		ahead(- getX()+ getWidth()/2);
		
		while (true) {
			turnRadarLeft(90);
			turnRadarRight(90);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {}
		}
	}
	
	@Override
	public void onScannedRobot(ScannedRobotEvent e) {
		String name = e.getName();
        if(!name.startsWith("sample.RoboScanner")){
        	double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);
            int scannedX = (int)(getX() + Math.sin(angle) * e.getDistance());
            int scannedY = (int)(getY() + Math.cos(angle) * e.getDistance());
            RoboScaneado rb = new RoboScaneado(name, e.getHeading(), scannedX, scannedY, false);
            addRoboScaneado(rb);
        }
		
//		//event.getRobotDistance()
//		EnvirMsgJason en = new EnvirMsgJason(_portaServidorTCPJason, _ipTCPJason, getRoboScaneado(event));
//		en.start();
	}
	
}

class EnvirMsgJason extends Thread {
	private int _portaServidorTCPJason;
	private String _ipTCPJason;
	private boolean _continuar = true;
	private RoboScaneado _rb;
	
	public EnvirMsgJason(int portaServidorTCPJason, String ipTCPJason, RoboScaneado rb){
		_portaServidorTCPJason = portaServidorTCPJason;
		_ipTCPJason = ipTCPJason;
		_rb = rb;
		_continuar = true;
	}
	
	public void Parar(){
		_continuar = false;
	}
	
//	public void run(){
//		TCPClient cli = new TCPClient(_portaServidorTCPJason, _ipTCPJason);
//	
//		while (_continuar) {
//			//System.out.println("# ROBO - AvisarJason() ");
//			cli.enviarDadosScaneados(_rb);
//			try { Thread.sleep(150); } catch (InterruptedException e) { e.printStackTrace(); } //aguarda um pouco
//		}
//		cli = null;
//	}
}